//
//  PywAbsolutedLoginPwdController.m
//  PywSdk
//
//  Created by smallwave on 16/8/30.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywAbsolutedLoginPwdController.h"
#import "UILabel+Pyw.h"
#import "UIButton+Pyw.h"
#import "PywTextFieldView.h"
#import "PywMasonry.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywSetSubAccountPwdApi.h"
#import "PywResetAbsolutePwdSuccessController.h"
#import "MutableDictionary.h"

@interface PywAbsolutedLoginPwdController ()

@property (nonatomic, strong) UILabel                   *gameAcccountLabel;     // 游戏账号Label
@property (nonatomic, strong) PywTextFieldView          *absolutePwdText;       // 独立密码Text
@property (nonatomic, strong) UIButton                  *sureBtn;               // 确定设置按钮
@property (nonatomic, strong) UILabel                   *reminderLabel;         // 温馨提示Label

@property (nonatomic, copy) NSString                    *account;               // 传入的游戏账号
@property (nonatomic, strong) PywSetSubAccountPwdApi    *subAccountSetPwdApi;   // 子游戏账号设置密码

@end

@implementation PywAbsolutedLoginPwdController
KeyBoardHelper(90)

#pragma mark - Init
- (instancetype)initWithGameAccount:(NSString *)account{
    self = [super init];
    if (self) {
        self.account = account;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
#pragma mark - prepareUI
- (void)setUp{
    self.title = @"重设独立密码";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.gameAcccountLabel];
    [self.view addSubview:self.absolutePwdText];
    [self.view addSubview:self.sureBtn];
    [self.view addSubview:self.reminderLabel];
    
    __weak typeof(self) wSelf = self;
    [self.gameAcccountLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
    }];
    [self.absolutePwdText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.gameAcccountLabel.mas_bottom).offset(20);
        make.width.equalTo(wSelf.gameAcccountLabel.mas_width);
        make.height.equalTo(@40);
    }];
    [self.sureBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.absolutePwdText.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.absolutePwdText);
    }];
    [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.sureBtn.mas_bottom).offset(20);
        make.width.equalTo(wSelf.sureBtn.mas_width);
    }];
}
#pragma mark - Action
- (void)sureSetAbsolutePwdAction{
    [self.view endEditing:YES];
    
    NSString *password = [self.absolutePwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (password.length == 0) {
        [PywMBProgressHUD showError:@"请输入密码！"];
    }else if (password.length < 6) {
        [PywMBProgressHUD showError:@"密码不能低于6位！"];
    }else {
        MutableDictionary *params = [self requestSubAccountSetPwdParams];
        [self.subAccountSetPwdApi requestSetSubAccountPwdWithParams:params success:^(NSString *status, id anyObjct) {
            
            [PywMBProgressHUD showSuccess:@"设置成功！" toView:self.view];
            PywResetAbsolutePwdSuccessController *successVC = [[PywResetAbsolutePwdSuccessController alloc] initWithGameAccount:self.account LoginPwd:self.absolutePwdText.text];
            [self.navigationController pushViewController:successVC animated:YES];
        } fail:^(NSError *error) {
            
        }];
    }
}
/**
 *  子账号设置登录密码  请求需要传入参数
 */
- (MutableDictionary *)requestSubAccountSetPwdParams{
    NSString *account = [self.account stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *account_pwd = [self.absolutePwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:account forKey:@"account"];
    [params setObject:account_pwd forKey:@"account_pwd"];
    return params;
}
#pragma mark - Lazy
- (UILabel *)gameAcccountLabel{
    if (!_gameAcccountLabel) {
        _gameAcccountLabel = [UILabel labelWithText:nil
                                      textAlignment:NSTextAlignmentLeft
                                           fontSize:14
                                          textColor:[UIColor blackColor]];
        
        NSString *propmtStr = [NSString stringWithFormat:@"游戏账号：%@",self.account];
        NSMutableAttributedString *propmtMessage = [[NSMutableAttributedString alloc] initWithString:propmtStr];
        [propmtMessage addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xFFBC00) range:NSMakeRange(5, self.account.length)];
        _gameAcccountLabel.attributedText = propmtMessage;
    }
    return _gameAcccountLabel;
}
- (PywTextFieldView *)absolutePwdText{
    if (!_absolutePwdText) {
        _absolutePwdText = [PywTextFieldView passwordTextFieldWithLeftImg:@"icon_password_normal"
                                                          TextPlaceholder:@"请输入独立密码(限6-12位)"
                                                          ButtonNormalImg:@"icon_visible_normal"
                                                        ButtonSelectedImg:@"icon_visible_disabled"];
        [_absolutePwdText setAccessibilityId:@"alone_pwsd"];
    }
    return _absolutePwdText;
}
- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithBackgroundColor:UIColorFromRGB(0xFFBC00)
                                          cornerRadius:20
                                            titleColor:[UIColor whiteColor]
                                    titleLabelFontSize:18
                                                 title:@"确定设置"];
        
        [_sureBtn addTarget:self action:@selector(sureSetAbsolutePwdAction) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn setAccessibilityIdentifier:@"alonepwsd_sure"];
    }
    return _sureBtn;
}
- (UILabel *)reminderLabel{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:@"温馨提示：独立密码和通行证密码不要相同"
                                  textAlignment:NSTextAlignmentCenter
                                       fontSize:12
                                      textColor:UIColorFromRGB(0x333333)];
    }
    return _reminderLabel;
}
- (PywSetSubAccountPwdApi *)subAccountSetPwdApi{
    if (!_subAccountSetPwdApi) {
        _subAccountSetPwdApi = [PywSetSubAccountPwdApi manager];
    }
    return _subAccountSetPwdApi;
}

@end
